package com.jiyun.jiyunmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jiyun.jiyunmall.common.service.impl.CrudServiceImpl;
import com.jiyun.jiyunmall.product.dao.CategoryBrandRelationDao;
import com.jiyun.jiyunmall.product.dto.CategoryBrandRelationDTO;
import com.jiyun.jiyunmall.product.entity.CategoryBrandRelationEntity;
import com.jiyun.jiyunmall.product.service.CategoryBrandRelationService;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 *
 *
 * @author boge 71164376@qq.com
 * @since 1.0.0 2022-08-04
 */
@Service
public class CategoryBrandRelationServiceImpl extends CrudServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity, CategoryBrandRelationDTO> implements CategoryBrandRelationService {
    // 逆向生成的，用于拼接查询条件的一个对象，最终转为WHERE去执行的
    @Override
    public QueryWrapper<CategoryBrandRelationEntity> getWrapper(Map<String, Object> params){
        String key = (String)params.get("id");
        String categoryId = (String)params.get("categoryId");


        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
        // WHERE id = key or brand_name like %key% and category_id=catId
        wrapper.eq(StringUtils.isNotBlank(key), "id", key);
        wrapper.or().like(StringUtils.isNotBlank(key),"brand_name",key);
        if(categoryId!=null && !categoryId.equals("0")&& !categoryId.equals("")){
            wrapper.and(w->{
                w.eq(StringUtils.isNotBlank(categoryId),"category_id",categoryId);
            });
        }

//        if(categoryId!=null && categoryId.equals("")){
//            wrapper.or().eq("category_id",categoryId);
//        }
        return wrapper;
    }
    @Autowired
    private CategoryBrandRelationDao categoryBrandRelationDao;

    @Override
    public void updateByBrandId(CategoryBrandRelationDTO categoryBrandRelationDTO) {
        categoryBrandRelationDao.updateByBrandId(categoryBrandRelationDTO);
    }
}